package trading.ctm.yunjing.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.json.JSONException;
import org.json.JSONObject;

import app.AppApplication;
import cc.vv.baselibrary.global.JNIBroadCastKey;
import cc.vv.baselibrary.util.LKToastUtil;


/**
 * 微信客户端回调activity示例
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI wxApi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wxApi = AppApplication.Companion.getMsgApi();
        wxApi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        wxApi.handleIntent(data, this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        wxApi.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                String CaoTaDaYe = new Gson().toJson(resp);
                String code = "";
                try {
                    JSONObject jsonObj = new JSONObject(CaoTaDaYe);
                    code = jsonObj.getString("code");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.obj = code;
                msg.what = 1;
                handler.sendMessage(msg);
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                handler.sendEmptyMessage(-1);
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                handler.sendEmptyMessage(-2);
                break;
            default:
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
//                    LKToastUtil.showToastShort("分享成功");
                    String code = msg.obj.toString();
                    Intent intent = new Intent(JNIBroadCastKey.SHARE_LINK_SUCCESS).putExtra("code", code);
                    sendBroadcast(intent);
                    finish();
                    break;
                case -1:
                    LKToastUtil.showToastShort("分享失败");
                    sendBroadcast(new Intent(JNIBroadCastKey.SHARE_LINK_LOSE));
                    finish();
                    break;
                case -2:
                    LKToastUtil.showToastShort("分享取消");
                    finish();
                    break;
                default:
            }
        }
    };

}
